package com.gzc.just.play.last.war.centralserver.message.handler;



import com.gzc.just.play.last.war.centralserver.guild.GuildManager;

import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.RequiredArgsConstructor;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Component;



import java.util.Map;



@Slf4j

@Component("central-guildHandler")
@RequiredArgsConstructor

public class GuildHandler implements MessageHandlerInterface {



    private final GuildManager guildManager;

    private final ObjectMapper objectMapper;



    @Override

    public void handle(Long playerId, Object messageData) {

        try {

            Map<String, Object> guildData = objectMapper.convertValue(messageData, Map.class);

            String action = (String) guildData.get("action");

            

            switch (action) {

                case "create_guild":

                    handleCreateGuild(playerId, guildData);

                    break;

                case "join_guild":

                    handleJoinGuild(playerId, guildData);

                    break;

                case "leave_guild":

                    handleLeaveGuild(playerId);

                    break;

                case "guild_member_action":

                    handleGuildMemberAction(playerId, guildData);

                    break;

                case "guild_upgrade":

                    handleGuildUpgrade(playerId);

                    break;

                default:

                    log.warn("Unknown guild action: {}", action);

            }

        } catch (Exception e) {

            log.error("Error processing guild message for player: {}", playerId, e);

        }

    }

    

    private void handleCreateGuild(Long playerId, Map<String, Object> data) {

        String guildName = (String) data.get("guildName");

        String guildDesc = (String) data.getOrDefault("guildDesc", "");

        

        guildManager.createGuild(playerId, guildName, guildDesc)

            .thenAccept(guild -> {

                if (guild != null) {

                    log.info("Player {} created guild: {}", playerId, guildName);

                } else {

                    log.warn("Failed to create guild {} for player: {}", guildName, playerId);

                }

            });

    }

    

    private void handleJoinGuild(Long playerId, Map<String, Object> data) {

        Long guildId = Long.valueOf(data.get("guildId").toString());

        

        guildManager.joinGuild(playerId, guildId)

            .thenAccept(success -> {

                if (success) {

                    log.info("Player {} joined guild: {}", playerId, guildId);

                } else {

                    log.warn("Failed to join guild {} for player: {}", guildId, playerId);

                }

            });

    }

    

    private void handleLeaveGuild(Long playerId) {

        guildManager.leaveGuild(playerId)

            .thenAccept(success -> {

                if (success) {

                    log.info("Player {} left guild", playerId);

                } else {

                    log.warn("Failed to leave guild for player: {}", playerId);

                }

            });

    }

    

    private void handleGuildMemberAction(Long playerId, Map<String, Object> data) {

        String memberAction = (String) data.get("memberAction");

        Long targetPlayerId = Long.valueOf(data.get("targetPlayerId").toString());

        

        switch (memberAction) {

            case "promote":

                guildManager.promoteMember(playerId, targetPlayerId);

                break;

            case "demote":

                guildManager.demoteMember(playerId, targetPlayerId);

                break;

            case "kick":

                guildManager.kickMember(playerId, targetPlayerId);

                break;

            default:

                log.warn("Unknown guild member action: {}", memberAction);

        }

    }

    

    private void handleGuildUpgrade(Long playerId) {

        guildManager.upgradeGuild(playerId)

            .thenAccept(success -> {

                if (success) {

                    log.info("Guild upgraded by player: {}", playerId);

                } else {

                    log.warn("Failed to upgrade guild for player: {}", playerId);

                }

            });

    }

}